package com.malred.services;

import com.malred.spring.Component;
import com.malred.spring.ComponentScan;
import com.malred.spring.beanPostProcessor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author malguy-wang sir
 * @create ---
 */
@Component
public class myPostProcessor implements beanPostProcessor {
    @Override
    public Object postProcessorBeforeInitialization(String beanName, Object clazz) {
        System.out.println("被拿到的bean: "+beanName);
        if(beanName.equals("order")){ System.out.println("这可是笔大订单"); }
        return clazz;}
    @Override
    public Object postProcessorAfterInitialization(String beanName, Object clazz) {
        System.out.println("被处理的bean: "+beanName);
        if(beanName.equals("order")){ System.out.println("我想你不会让我失望"); }
        if(beanName.equals("userService")){
            Object proxyInstance = Proxy.newProxyInstance(myPostProcessor.class.getClassLoader(),
                    clazz.getClass().getInterfaces(),
                    new InvocationHandler() {
                        @Override
                        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                            System.out.println("切面逻辑");//执行代理对象方法
                            return method.invoke(clazz,args);//执行原本的对象的方法
                        }
                    });
            return proxyInstance;
        }
        return clazz; }
}
